﻿@page "/FileView/{Text?}"
@using WebFile.Share.Data;
@using Microsoft.EntityFrameworkCore
@using Path = System.IO.Path
@inject IDbContextFactory<WebFileContext> DbContext
@inject NavigationManager NavigationManager
@inject MessageService Message
@inject AuthenticationStateProvider AuthStateProvider

@if (!string.IsNullOrEmpty(Model.Path))
{
    <PageTitle>@Path.GetFileName(Model.Path)</PageTitle>

    <Card>
        <BodyTemplate>
            <FileIcon Extension=@Path.GetExtension(Model.Path)/>
            <h5>@Path.GetFileName(Model.Path)</h5>
            <p>文件大小: @Info?.Size</p>
            <Button IsAsync="true" Text="返回" OnClick="@GoBack"></Button>
        </BodyTemplate>
    </Card>

    <Divider/>

    @if (Path.GetExtension(Model.Path) == ".pdf")
    {
        <PdfReader FileName="@Model.GetUrlWithoutWWW()"/>
    }
    else if (Path.GetExtension(Model.Path) is ".docx" or ".doc")
    {
        <FileViewer Filename="@Model.GetUrl()" IsExcel="false"/>
    }
    else if (Path.GetExtension(Model.Path) is ".xlsx" or ".xls")
    {
        <FileViewer Filename="@Model.GetUrl()" IsExcel="true"/>
    }
    else if (Path.GetExtension(Model.Path).IsImage())
    {
        <ImageViewer PreviewList="@(new List<string> { Model.GetUrlWithoutWWW() })" Url="@Model.GetUrlWithoutWWW()"/>
    }
    else if (Path.GetExtension(Model.Path).IsVideo())
    {
        <VideoPlayer MineType="@Model.GetUrlWithoutWWW()" Url="@Model.GetUrl()" Width="500" Height="380"/>
    }
    else if (IsCode)
    {
        <div class="code">
            <Select DisplayText="主题" TValue="string" @bind-Value="@Theme" ShowLabel="true">
                <Options>
                    <SelectOption Text="Visual Studio" Value="vs"/>
                    <SelectOption Text="Visual Studio Dark" Value="vs-dark"/>
                    <SelectOption Text="High Contrast Dark" Value="hc-black"/>
                </Options>
            </Select>
            <Divider/>
            <CodeEditor IsReadonly="true" Value="@CodeContext" Language="@Lang" Theme="@Theme"/>
        </div>
    }
    else
    {
        <Card IsCenter="true">
            <BodyTemplate>
                <Empty Image="empty.svg" Width="133" Text="@($"该格式({Path.GetExtension(Model.Path)})的文件目前还无法预览")"/>
                <Button OnClick="@(() => NavigationManager.NavigateTo(""))">返回主页</Button>
            </BodyTemplate>
        </Card>
    }
}
else
{
    <PageTitle>未找到</PageTitle>
    <Empty Image="https://gw.alipayobjects.com/zos/antfincdn/ZHrcdLPrvN/empty.svg" Width="133" Text="暂无数据">
        <Template>
            <Button OnClick="@(() => NavigationManager.NavigateTo(""))">返回主页</Button>
        </Template>
    </Empty>
}

<style>
    .code{
        margin: 20px;
    }
</style>